<?
$mmargin = $_MODULE['mmargin'] ? "margin-bottom:" . $_MODULE['mmargin'] . "px;" : "";
$mmcolor = $_MODULE['mmcolor'] ? "background:" . $_MODULE['mmcolor'] . ";" : "";

$sortKeyMap = array("posting"=>"gmtCreate","promPiecePrice"=>"piecePrice","sales"=>"soldCount","price"=>"minPrice");

function getSortKey($sort){
    global $sortKeyMap;
    $array = explode("ASC",$sort);
    if(count($array) > 1){
        $K1 = $array[0];
        return $sortKeyMap[$K1];
    }else{
        $k = explode("Desc", $sort)[0];
        return $sortKeyMap[$k];
    }
}



function getOrderByType($sort){
    $index = strpos($sort,'ASC');
    if($index){
        return SORT_ASC;
    }else{
        return SORT_DESC;
    }
}
?>


<div class="module dshop-um-items4-2 module-thirdparty" style="<? echo $mmargin . $mmcolor; ?>">

    <div class="content">

        <?
        $pics = pic_250();

        $bdis = $_MODULE[bdis];
        $color = explode('|', $_MODULE[bcolor]);
        if ($_MODULE[bcolor]) {
            $bcolor = "color:" . $_MODULE[bcolor] . ";";
        } else {
            $bcolor = "";
        }
        if ($_MODULE[bipic]) {
            $bipic = "background:url(" . $_MODULE[bipic] . ") no-repeat top center;border:none;";
        } else {
            $bipic = "";
        }
        $hddis = $_MODULE[hddis];

        if ($hddis == "none") {
            $bipic2 = "background:none;";
        } else {

            $bipic1 = $bipic;
        }

        echo '
		<div class="hd" style="' . $bipic1 . '' . $bipic2 . $bdis . '">
			<a href="' . $_MODULE[bghreftext] . '">
			<div class="title1" style="' . $bcolor . '">' . $_MODULE[bgtext] . '</div>
			</a>
		</div>';

        ?>
        <?
        $istrue = '@@' . $_MODULE['baodis'];
        if (!strpos($istrue, 'title')) {      //标题
            $title_dis = 'display:none;';
        }
        if (!strpos($istrue, 'now')) {        //现价
            $title_now = 'display:none;';
        }
        if (!strpos($istrue, 'old')) {        //原价
            $title_old = 'display:none;';
        }
        if (!strpos($istrue, 'sold')) {       //销量
            $title_sold = 'display:none;';
        }
        if (!strpos($istrue, 'off')) {        //折扣
            $title_off = 'display:none;';
        }
        if (!strpos($istrue, 'buy')) {        //折扣
            $title_buy = 'display:none;';
        }


        if ($_MODULE[title]) {
            $title_title = "color:" . $_MODULE[title] . ";";
        } else {
            $title_title = "";
        }
        if ($_MODULE[nowtext]) {
            $nowtext = "color:" . $_MODULE[nowtext] . ";";
        } else {
            $nowtext = "";
        }
        if ($_MODULE[oldtext]) {
            $oldtext = "color:" . $_MODULE[oldtext] . ";";
        } else {
            $oldtext = "";
        }
        if ($_MODULE[soldtext]) {
            $soldtext = "color:" . $_MODULE[soldtext] . ";"
		} else {
            $soldtext = "";
        }

        if ($_MODULE[newscolor]) {
            $newscolor = "background:" . $_MODULE[newscolor] . ";";
        } else {
            $newscolor = "";
        }
        if ($_MODULE[buycolor]) {
            $buycolor = "background:" . $_MODULE[buycolor] . ";";
        } else {
            $buycolor = "";
        }
        if ($_MODULE[buytextcolor]) {
            $buytextcolor = "color:" . $_MODULE[buytextcolor] . ";";
        } else {
            $buytextcolor = "";
        }
        $buytext = $_MODULE[buytext];

        $zdq = explode('|', $_MODULE[zdq]);

        $dis_pic = $_MODULE['dis_pic'] == 1 ? "display:none;" : "";
        $mask_type = $_MODULE['mask_type'];//鼠标划过效果
        $pic_type = $_MODULE['pic_type'];//图片切换效果
        $picsize = $_MODULE['picsize'];
        if ($_MODULE['height1']) {
            $height1 = $_MODULE['height1'];
        } else {
            $height1 = 200;
        }
        $ids = explode(',', $_MODULE[items]);

        $sort = $_MODULE['sort'] == 'total_salepro' ? "salesDesc" : $_MODULE['sort'];


        $num = $_MODULE[num];
        $style1 = "height:" . $height1 . "px;";

        $sort = $_MODULE['sort'] == 'total_salepro' ? "salesDesc" : $_MODULE['sort'];

        $getMode = $_MODULE['get_mode'];

        $discountsArray = [];
        //折扣角标设置
        $discounts = $_MODULE['discounts'];
        if(!empty($discounts)){
            $discountsArray = explode('|',$discounts);
        }

        $items = array();
        if($getMode == 'custom'){
            if ($ids != null) {
                if ($sort == "none") {
                    foreach ($ids as $id) {
                        $item = $itemManager->queryById($id);
                        array_push($items, $item);
                    }
                } else {
                    $items = $itemManager->queryByIds($ids, $sort);
                }

            }
        }elseif ($getMode == 'category'){
            $categories = explode(',', $_MODULE['categories']);
            foreach ($categories as $category){
                $newItems = $itemManager->queryByProductGroup($category,"salesDesc",10);
                $items  = array_merge($items, $newItems);
            }

            $sortDesc = "";
            if($sort == "none"){
                $sortDesc = "postingDesc";
            }else{
                $sortDesc = $sort;
            }

            $orderByType = getOrderByType($sortDesc);
            $sortKey = getSortKey($sortDesc);

            $flag = array();
            foreach ($items as $s) {
                $flag[] = $s[$sortKey];
            }

            array_multisort($flag, SORT_DESC, $items);
        }

        if($getMode == 'auto' || $items == null || count($items) == 0){
            $items = $itemManager->queryByUserId($sort, 20);
        }


        $num = $_MODULE[num];
        $style1 = "height:" . $height1 . "px;";

        echo '

	<div class="bd">
		<ul class="items">';

        for ($i = 0; $i < $num; $i++) {
            if ($items[0]) {
                if ($items[$i]) {
                    $title = $items[$i]->title;//商品标题
                    $soldCount = $items[$i]->soldCount;//商品销量

                    $minPrice1 = $items[$i]->minPrice;
                    $unit = $items[$i]->productUnit;//商品单位

                    $lotNum = $items[$i]->lotNum;
                    if ($lotNum != 0) {
                        $minPrice = sprintf("%.2f", $minPrice1 / $lotNum);
                    } else {
                        $minPrice = $minPrice1;
                    }

                    //商品折扣
                    $maxPrice = number_format($items[$i]->maxPrice, $_MODULE[xshu]);
                    if ($items[$i]->discount != null) {

                        $discount = $items[$i]->discount;

                    } else {
                        $discount = '0';

                    }
                    $off = floor(100 - $discount);
                    if ($off != 0) {
                        $price1 = number_format($minPrice * ($off) * 0.01, $_MODULE[xshu]);

                    } else {
                        $price1 = number_format($minPrice, $_MODULE[xshu]);
                    }
                    $a = $i + 1;
                    if ($_MODULE['pic' . $a] == null || $_MODULE['pic' . $a] == 0) {

                        $pic = $items[$i]->getScalePicUrl($picsize);//商品图片
                    } else {
                        $pic = $_MODULE['pic' . $a];//商品图片
                    }
                    $url = $items[$i]->detailUrl;

                } else {
                    break;
                }

            } else {
                $title = "Summer new arrival 2014 vintage embroidered paragraph batwing sleeve shirt female t1827";
                $url = "#";
                $pic = $pics[$i];
                $price1 = "8.99";
                $discount = 30;
                $soldCount = 40;
                $unit = "piece";
            }


            if (($i + 1) % 3 == 0) {
                $style = "margin-right:0px;";

            } else {
                $style = "";
            }

            $min_dis = $minPrice ? "display:block;" : "display:none;";

            $title_off_discount = '';

            if(count($discountsArray) > $i){
                $isShowDiscount = $discountsArray[$i];
                if(!empty($isShowDiscount) && $isShowDiscount == '0') {
                    $title_off_discount = $title_off;
                }else {
                    $title_off_discount = 'display:none;';
                }

            }


            echo '<li class="lin" style="' . $style . ' ">
				<div class="off" style="' . $title_off_discount . '">' . $discount . '</div>
				<div class="pic" style="' . $style1 . '">';

            echo '  
					<a target="_blank" href="' . $url . '" title="' . $title . '" style="background:url(' . $pic . ') no-repeat ' . $zdq[0] . ' ' . $zdq[1] . ';' . $style1 . '">
					 	
						<div class="img hover hover' . $pic_type . '"  style="background:url(' . $pic . ') no-repeat ' . $zdq[0] . ' ' . $zdq[1] . ';' . $dis_pic . $style1 . '"></div>
						<s class="mask ' . $mask_type . '"></s>
					
					</a>';


            echo '
				</div>
				<div class="deat" style="' . $newscolor . '">
					<div class="desc" style="' . $title_dis . '">
						<a target="_blank" href="' . $url . '" style="' . $title_title . '" >' . $title . ' </a>
					</div>
					

					<a  class="but" href="' . $url . '" target="_blank">
						<div class="price">
							<span class="pr1" style="' . $nowtext . '' . $title_now . '">US $<em>' . $price1 . '</em>piece</span>
							<span class="pr2" style="' . $oldtext . '' . $title_old . '">$' . $minPrice . '</span>
						</div>

						<div class="bus" style="' . $buytextcolor . '' . $buycolor . '' . $title_buy . '">' . $buytext . '</div>
						
					</a>
					
				</div>
			</li>';


        }
        echo '</ul>
	</div>';
        ?>
    </div>
</div>